package org.game.module.chatbo.testcmd;

import com.google.common.collect.Maps;
import com.sojoys.artifact.manager.MessageManager;
import com.sojoys.artifact.tools.ToolError;
import com.sojoys.artifact.tools.ToolList;
import org.cfg.BuildCfg;
import org.constant.EventConditionKey;
import org.constant.EventConditionType;
import org.constant.EventType;
import org.error.GameErrorCode;
import org.game.constant.BuildingConstant;
import org.game.db.bean.Building;
import org.game.db.bean.Player;
import org.game.db.cache.BuildingCache;
import org.game.log.LogBuilding;
import org.game.log.builder.LogDsp;
import org.game.log.dsp.BuildingDsp;
import org.game.module.buildingbo.BuildingBO;
import org.game.msgBuilder.BuildingMsgBuilder;
import org.game.plugin.event.GameEvent;
import org.game.plugin.event.GameEventPlugin;
import org.game.protobuf.s2c.S2CBuildingMsg;
import org.game.util.ProtoUnit;

import java.util.List;
import java.util.Map;

/**
 * Created by fxf on 2018-4-9.
 */
public class BuildingTest {
    public static byte[] dispatcher(Player player, List<String> cmds) {
        int index = ToolList.getInt(1, cmds);
        byte[] result = new byte[0];
        switch (index) {
            case 1:
                return upGradeBuilding(player,cmds);
            case 2:
                break;
            case 3:
                break;
        }
        return result;
    }

    public static byte[] upGradeBuilding(Player player, List<String> cmds){
        int buildingCid = ToolList.getInt(2, cmds);
        Building building = BuildingCache.me().getByPlayerIdCid(player.getId(),buildingCid);
        ToolError.isAndTrue(GameErrorCode.CLIENT_PARAM_IS_ERR,"buildingCid 异常",building == null);
        if (building != null){
            int oldLvl = building.getBuildingProxy().getCfg().getLevel();
            BuildCfg nextLevelCfg = building.getBuildingProxy().getNextLevelCfg();
            building.setFinishTime(null);
            building.setState(BuildingConstant.BUIDING_STATE_UPGRADE_FINISH);
            building.setCid(nextLevelCfg.getId());
            building.update();
        }

        S2CBuildingMsg.UpdateBuildingInfo updateBuildingInfo = BuildingMsgBuilder.buildUpdateBuildingInfoMsg(building,S2CBuildingMsg.BuildingEvent.UPGRADE_FINISH);
        player.getPlayerProxy().sendMsg(MessageManager.me().create(BuildingBO.updateBuilding, ProtoUnit.toByte(updateBuildingInfo)));

        return new byte[0];
    }
}
